#import "MyView.h"

@interface UITouch(KeyForDictionary)

- (NSString *)keyString;

@end

@implementation MyView

- (id) initWithFrame:(CGRect)frame
{
	
	if((self = [super initWithFrame:frame])) {
		strokes = [[StrokeList alloc] init];
		
		[self setDelegate:self];
	}
	
	return self;
}

- (void) dealloc {
	[strokes release];
	[super dealloc];
}

- (void) drawRect:(CGRect)clip {
	[strokes strokeAllPath:UIGraphicsGetCurrentContext()];
}

- (void)gestureView:(GestureView*)view touchBegan:(UITouch *)touch {
	[strokes recordPoint:[touch locationInView] forKey:[touch keyString]];
	[self setNeedsDisplay];
}

- (void)gestureView:(GestureView*)view touchMoved:(UITouch *)touch {
	[strokes recordPoint:[touch locationInView] forKey:[touch keyString]];
	[self setNeedsDisplay];
}

- (void)gestureView:(GestureView*)view touchEnded:(UITouch *)touch {
	[strokes commitRecording];
	[self setNeedsDisplay];
}

- (void)gestureView:(GestureView*)view touchCanceled:(UITouch *)touch {
	[strokes rollbackRecordingForKey:[touch keyString]];
	[self setNeedsDisplay];
}

- (void)gestureViewEventCanceled:(GestureView*)view {
	[strokes rollbackRecording];
	[self setNeedsDisplay];
}

- (void)gestureView:(GestureView*)view tappedAt:(CGPoint)location count:(NSUInteger)tapCount {
	if (tapCount == 2) {
		[strokes clear];
		[self setNeedsDisplay];
	}
}

@end

@implementation UITouch(KeyString)

- (NSString *)keyString	{
	return [NSString stringWithFormat:@"%@", self];
}

@end
